package com.ruoyi.training.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.training.dto.StudentNoticeDto;
import com.ruoyi.training.vo.StudentNoticeVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.training.domain.StudentNotice;
import com.ruoyi.training.service.IStudentNoticeService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 学员通知Controller
 *
 * @author lqx
 * @date 2025-10-24
 */
@RestController
@RequestMapping("/studentNotice")
public class StudentNoticeController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(StudentNoticeController.class);

    @Autowired
    private IStudentNoticeService studentNoticeService;

    /**
     * 查询学员通知列表
     */
    @RequiresPermissions("training:studentNotice:list")
    @GetMapping("/list")
    public TableDataInfo list(StudentNotice studentNotice) {
        startPage();
        List<StudentNotice> list = studentNoticeService.selectStudentNoticeList(studentNotice);
        return getDataTable(list);
    }

    /**
     * 通过班级id查询通知详情
     * lqx
     */
    @GetMapping("/listByClassId/{trainingClassId}")
    public TableDataInfo listByClassId(@PathVariable("trainingClassId") String trainingClassId) {
        startPage();
        List<StudentNoticeVo> list = studentNoticeService.selectListByClassId(trainingClassId);
        return getDataTable(list);
    }

    /**
     * 导出学员通知列表
     */
    @RequiresPermissions("training:studentNotice:export")
    @Log(title = "学员通知", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StudentNotice studentNotice) {
        List<StudentNotice> list = studentNoticeService.selectStudentNoticeList(studentNotice);
        ExcelUtil<StudentNotice> util = new ExcelUtil<StudentNotice>(StudentNotice.class);
        util.exportExcel(response, list, "学员通知数据");
    }

    /**
     * 获取学员通知详细信息
     */
    @RequiresPermissions("training:studentNotice:query")
    @GetMapping(value = "/{studentNoticeId}")
    public AjaxResult getInfo(@PathVariable("studentNoticeId") String studentNoticeId) {
        return success(studentNoticeService.selectStudentNoticeByStudentNoticeId(studentNoticeId));
    }

    /**
     * 新增学员通知
     */
    @RequiresPermissions("training:studentNotice:add")
    @Log(title = "学员通知", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StudentNotice studentNotice) {
        return toAjax(studentNoticeService.insertStudentNotice(studentNotice));
    }

    /**
     * 学员通知处理
     * lqx
     */
    @Log(title = "学员通知", businessType = BusinessType.INSERT)
    @PostMapping("/send")
    public AjaxResult studentNoticeProcess(@RequestBody StudentNoticeDto studentNoticeDto) {
        int resultCode;
        try {
            resultCode = studentNoticeService.studentNoticeProcess(studentNoticeDto);

            // 根据返回的状态码返回对应信息
            if (resultCode == 100) {
                return AjaxResult.success("发送成功");
            } else if (resultCode == 90) {
                return AjaxResult.error("当前班级下学员为空或指定学员为空，请检查");
            } else if (resultCode == 91) {
                return AjaxResult.error("未查询到有效学员信息");
            } else {
                // 处理未定义的状态码
                return AjaxResult.error("通知处理失败，未知状态码：" + resultCode);
            }
        } catch (Exception e) {
            log.error("学员通知处理异常", e);
            // 返回异常提示
            return AjaxResult.error("通知处理失败：" + e.getMessage());
        }
    }

    /**
     * 修改学员通知
     */
    @RequiresPermissions("training:studentNotice:edit")
    @Log(title = "学员通知", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StudentNotice studentNotice) {
        return toAjax(studentNoticeService.updateStudentNotice(studentNotice));
    }

    /**
     * 删除学员通知
     */
    @RequiresPermissions("training:studentNotice:remove")
    @Log(title = "学员通知", businessType = BusinessType.DELETE)
    @DeleteMapping("/{studentNoticeIds}")
    public AjaxResult remove(@PathVariable String[] studentNoticeIds) {
        return toAjax(studentNoticeService.deleteStudentNoticeByStudentNoticeIds(studentNoticeIds));
    }
}
